﻿/*创建人：盛金龙
 *时间：2011/12/30 周五
 *用途：建设单位删改查
 */
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EnvironmentManagerSystem.MODE;
using EnvironmentManagerSystem.Utility;
using System.Text.RegularExpressions;

namespace EnvironmentManagerSystem.ConstructionUnit
{
    public partial class ConstructionUnit : System.Web.UI.Page
    {
        public Boolean isAdmin = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (StfInfo.ReSession()) return;if (!IsPostBack)
            {
                //是否是管理员并操作isAdmin
                BingDgv();
            } 
            
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        public void BingDgv()
        {
            gdvConstructionUnit.DataSource = EnvironmentManagerSystem.BLL.ConstructionUnit.GetConstructionUnit(hidUnitName.Value,hidlianxiren.Value,hidren.Value);
            gdvConstructionUnit.DataBind();
        }

        //分页事件
        protected void gdvConstructionUnit_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gdvConstructionUnit.PageIndex = e.NewPageIndex;
        }

        //分页后事件
        protected void gdvConstructionUnit_PageIndexChanged(object sender, EventArgs e)
        {
            BingDgv();
        }

        //删除事件
        protected void gdvConstructionUnit_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            EnvironmentManagerSystem.BLL.ConstructionUnit.DeleteConstructionUnit(gdvConstructionUnit.DataKeys[e.RowIndex].Value.ToString());
            BingDgv();
        }

        //编辑事件
        protected void gdvConstructionUnit_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gdvConstructionUnit.EditIndex = e.NewEditIndex;
            BingDgv();
        }

        //取消编辑
        protected void gdvConstructionUnit_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gdvConstructionUnit.EditIndex = -1;
            BingDgv();
        }

        //更新事件
        protected void gdvConstructionUnit_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (Check(e.RowIndex))
            {
                ConstructionUnitData cud = new ConstructionUnitData();
                cud.Name = ((TextBox)gdvConstructionUnit.Rows[e.RowIndex].Cells[0].Controls[0]).Text.Trim();
                cud.Address = ((TextBox)gdvConstructionUnit.Rows[e.RowIndex].Cells[1].Controls[0]).Text.Trim();
                cud.Phone = ((TextBox)gdvConstructionUnit.Rows[e.RowIndex].Cells[2].Controls[0]).Text.Trim();
                cud.ZipCode = ((TextBox)gdvConstructionUnit.Rows[e.RowIndex].Cells[3].Controls[0]).Text.Trim();
                cud.State = ((TextBox)gdvConstructionUnit.Rows[e.RowIndex].Cells[4].Controls[0]).Text.Trim();
                cud.ApplicationTime = ((TextBox)gdvConstructionUnit.Rows[e.RowIndex].Cells[5].FindControl("TextBox1")).Text.Trim();
                cud.Lianxiren = ((TextBox)gdvConstructionUnit.Rows[e.RowIndex].Cells[6].Controls[0]).Text.Trim();
                cud.Ren = ((TextBox)gdvConstructionUnit.Rows[e.RowIndex].Cells[7].Controls[0]).Text.Trim();
                cud.ID = int.Parse(gdvConstructionUnit.DataKeys[e.RowIndex].Value.ToString());
                if (EnvironmentManagerSystem.BLL.ConstructionUnit.UpdateConstructionUnit(cud))
                {
                    gdvConstructionUnit.EditIndex = -1;
                    BingDgv();
                    Jscript.Alert("修改成功", this);
                }
                else
                {
                    Jscript.Alert("修改失败", this);
                }
            }
        }
        /// <summary>
        /// 验证文本框
        /// </summary>
        public Boolean Check(int i)
        {
            if (!((TextBox)gdvConstructionUnit.Rows[i].Cells[2].Controls[0]).Text.Trim().Equals("") && !((TextBox)gdvConstructionUnit.Rows[i].Cells[5].FindControl("TextBox1")).Text.Trim().Equals(""))
            {
                if (Regex.IsMatch(((TextBox)gdvConstructionUnit.Rows[i].Cells[2].Controls[0]).Text.Trim(), @"^((1\d{10})|(1\d{10}[, ，]))+$"))
                {
                    return true;
                }
                else
                {
                    Jscript.Alert("电话号码输入错误", this);
                    return false;
                }
            }
            else
            {
                Jscript.Alert("联系电话与时间不能为空", this);
                return false;
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            foreach (Control con in form1.Controls)
            {
                if (con is TextBox)
                    ((TextBox)con).Text = string.Empty;
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            hidUnitName.Value = txtUnitName.Text.Trim();
            hidlianxiren.Value = txtlianxiren.Text.Trim();
            hidren.Value = txtren.Text.Trim();
            BingDgv();
        }

        protected void gdvConstructionUnit_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            OfficeHelper.HelpDv(e.Row);
        }
    }
}
